//
//  AddressManageController.swift
//  baihuiju
//
//  Created by 赵家彬 on 2018/6/8.
//  Copyright © 2018年 赵家彬. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class AddressManageController: CBBaseViewController {
    
    //收货人姓名
    @IBOutlet weak var tfName: UITextField!
    //手机号
    @IBOutlet weak var tfMobile: UITextField!
    //区域
    @IBOutlet weak var lblArea: UILabel!
    //地址
    @IBOutlet weak var tfAddress: UITextField!
    //文字提示
    @IBOutlet weak var lblTips: UILabel!
    //按钮
    @IBOutlet weak var btnChange: UIButton!
    //选择区域
    var selectArea:Variable<(title:String, value:String)> = Variable((title:"请选择区域",value:""))
    weak var updateUserInfo:Variable<Bool>?
    var userModel:UserModel?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "地址管理"
        
        let addressVm = AddressManageViewModel(name: tfName.rx.observe(String.self, "text").asObservable(), mobile: tfMobile.rx.observe(String.self, "text").asObservable(), area: selectArea.asObservable(), address: tfAddress.rx.observe(String.self, "text").asObservable(), changeTap: btnChange.rx.tap.asObservable())
        
        addressVm.valid
            .subscribe(onNext:{ [weak self] info in
                self?.btnChange.isEnabled = info.isValid
                self?.btnChange.alpha = info.isValid ? 1.0 : 0.5
                self?.lblTips.text = info.tips
            }).disposed(by: disposeBag)
        
        addressVm.changeAddress
            .subscribe(onNext:{ [weak self] info in
                SVProgressHUD.dismiss()
                self?.updateUserInfo?.value = true
            }).disposed(by: disposeBag)
        
        selectArea.asObservable()
            .subscribe(onNext:{ [weak self] area in
                self?.lblArea.textColor = area.value.isEmpty ? UIColor(hex:0xcacacf) : .black
                self?.lblArea.text = area.title
            }).disposed(by: disposeBag)
        
        //屏幕点击释放
        let viewTap = UITapGestureRecognizer()
        viewTap.rx.event.subscribe(onNext:{ [unowned self] _ in
            self.view.endEditing(true)
        }).disposed(by: disposeBag)
        view.addGestureRecognizer(viewTap)
        
        //请求网络获得默认数据
        NetworkTool.requestData(url: ActionURL.GET_ADDRESS_INFO, param: ["uid":userModel?.uid ?? ""])
            .map { (json) -> AddressModel? in
                SVProgressHUD.show()
                return AddressModel.mj_object(withKeyValues: json["rows"].dictionaryObject)
            }.catchErrorJustReturn(nil)
            .observeOn(MainScheduler.instance)
            .subscribe(onNext: { [unowned self] opModel in
                SVProgressHUD.dismiss()
                if let model = opModel {
                    self.tfName.text = model.name
                    self.tfMobile.text = model.mobile
                    self.tfAddress.text = model.address
                    var strTitle = ""
                    var strValue = ""
                    if !model.provinceId.isEmpty && !model.cityId.isEmpty && !model.areaId.isEmpty {
                        strTitle = model.areaShow
                        strValue = "\(model.provinceId)|\(model.cityId)|\(model.areaId)"
                    } else {
                        strTitle = "请选择区域"
                        strValue = ""
                    }
                    self.selectArea.value = (title:strTitle,value:strValue)
                }
            }).disposed(by: disposeBag)
    }
    
    @IBAction func selectArea(_ sender: UITapGestureRecognizer) {
        let selectVc = AddressSelectController()
        selectVc.selectArea = selectArea
        self.navigationController?.pushViewController(selectVc, animated: true)
    }
    

}
